<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7 0007
 * Time: 16:27
 */

namespace Admin\Controller;

use Think\Page;

class ClientController extends AdminController
{

    public function add()
    {
        $this->display();
    }

    public function request()
    {
        $p = I('p', '1');
        $finish = I('finish', '0');
        $where = array('type' => '需求方', 'finish' => $finish);
        $page_size = 10;
        $Main_info = M('Main_info');
        $count = $Main_info->where($where)->count();
        $list = $Main_info->where($where)->order('modify_time desc')->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }

    public function supply()
    {
        $p = I('p', '1');
        $finish = I('finish', '0');
        $where = array('type' => '供应方', 'finish' => $finish);
        $page_size = 10;
        $Main_info = M('Main_info');
        $count = $Main_info->where($where)->count();
        $list = $Main_info->where($where)->order('modify_time desc')->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }

    public function finish()
    {
        $p = I('p', '1');
        $where = array('finish' => 1);
        $page_size = 10;
        $Main_info = M('Main_info');
        $count = $Main_info->where($where)->count();
        $list = $Main_info->where($where)->order('modify_time desc')->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }

    public function detail()
    {
        $id = I('id', '');
        $count = M('Main_info')->where(array('id' => $id))->count();
        if ($count) {
            $client = M('Main_info')->where(array('id' => $id))->find();
            $temp_data = explode(",", $client['major'], -1);
            foreach ($temp_data as $i => $value) {
                if ($value == '租赁') {
                    $client['majors']['a'] = 1;
                } else if ($value == '装卸') {
                    $client['majors']['b'] = 1;
                } else if ($value == '吊装') {
                    $client['majors']['c'] = 1;
                } else if ($value == '移动') {
                    $client['majors']['d'] = 1;
                } else if ($value == '搬迁') {
                    $client['majors']['e'] = 1;
                } else if ($value == '物流') {
                    $client['majors']['f'] = 1;
                }
            }
            $detail = M('Info_detail')->where('info_id=' . $client['id'])->field('message')->find();
            if ($detail) {
                $client['message'] = $detail['message'];
            }
            $this->assign('client', $client);
            $this->display();
        } else {
            $this->error('查询失败!');
        }
    }

    public function update()
    {
        $id = I('id', '');
        $data = array();
        $data['title'] = I('title', '');
        $data['release'] = I('release', date('Y-m-d'));
        $data['type'] = I('type', '');
        $temp_data = I('major_box');
        foreach ($temp_data as $count => $value) {
            $data['major'] .= $value . ',';
        }
        $data['validity'] = I('validity', date('Y-m-d'));
        $data['origin'] = I('origin', '');
        $data['destination'] = I('destination', '');
        $data['car'] = I('car', '');
        $data['weight'] = I('weight', '0');
        $data['price'] = I('price', '0');
        $data['company'] = I('company', '');
        $data['telphone'] = I('telphone', '');
        $data['contact'] = I('contact', '');
        $data['mail'] = I('mail', '');
        $data['fax'] = I('fax', '');
        $data['finish'] = I('finish', '0');
        $data['show'] = I('show', '0');
        $data['address'] = I('address', '');
        $data['lat'] = I('lat', '');
        $data['lang'] = I('lang', '');
        $data['create_time'] = I('create_time', date('Y-m-d H:i:s'));
        $data['modify_time'] = date('Y-m-d H:i:s');
        $message = I('message', '');
        $client_msg = M('Main_info');
        $count = $client_msg->where('id=' . $id)->count();
        if ($count) {
            $data['modify_uid'] = UID;
            $res = $client_msg->where('id=' . $id)->data($data)->save();
            if ($res) {
                $info_data['message'] = $message;
                $info = M('Info_detail')->where('info_id=' . $id)->find();
                if ($info) {
                    if ($info['message']!=$message) {
                        M('Info_detail')->where('info_id=' . $id)->data($info_data)->save();
                    }
                    $this->success('修改成功!', 'index.php?s=/Admin/Client/request');
                } else {
                    $info_data['info_id'] = $id;
                    M('Info_detail')->data($info_data)->add();
                    $this->success('修改成功!', 'index.php?s=/Admin/Client/request');
                }
            } else {
                $this->error('修改失败!', 'index.php?s=/Admin/Client/request');
            }
        } else {
            $uid = UID;
            $data['create_uid'] = $uid;
            $data['modify_uid'] = $uid;
            $info_data['message'] = $message;
            $res = $client_msg->data($data)->add();
            if ($res) {
                if ($message != '') {
                    $info_data['info_id'] = $res;
                    M('Info_detail')->data($info_data)->add();
                }
                $this->success('添加成功!', 'index.php?s=/Admin/Client/request');
            } else {
                $this->error('添加失败!', 'index.php?s=/Admin/Client/request');
            }
        }
    }

    public function delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Main_info')->where('id=' . $id)->delete();
            M('Info_detail')->where('info_id=' . $id)->delete();
            if ($res) {
                $this->success("删除成功!");
            } else {
                $this->error("删除失败！");
            }
        } else {
            $this->error("删除失败");
        }

    }

    public function mark()
    {
        $Order = M('Main_info');
        $id = I('id', '');
        $data = $Order->where("id=" . $id)->data('finish=1')->save();
        if ($data) {
            $this->success("修改成功!");
        } else {
            $this->error("修改失败!");
        }
    }

    public function edit()
    {
        $id = I('id', '');
        $count = M('Main_info')->where(array('id' => $id))->count();
        if ($count) {
            $client = M('Main_info')->where(array('id' => $id))->find();
            $temp_data = explode(",", $client['major'], -1);
            foreach ($temp_data as $i => $value) {
                if ($value == '租赁') {
                    $client['majors']['a'] = 1;
                } else if ($value == '装卸') {
                    $client['majors']['b'] = 1;
                } else if ($value == '吊装') {
                    $client['majors']['c'] = 1;
                } else if ($value == '移动') {
                    $client['majors']['d'] = 1;
                } else if ($value == '搬迁') {
                    $client['majors']['e'] = 1;
                } else if ($value == '物流') {
                    $client['majors']['f'] = 1;
                }
            }
            $detail = M('Info_detail')->where('info_id=' . $client['id'])->field('message')->find();
            if ($detail) {
                $client['message'] = $detail['message'];
            }
            $this->assign('client', $client);
            $this->display();
        } else {
            $this->error('查询失败!');
        }
    }

    public function release()
    {
        $id = I('id', '');
        $show = I('show', '0');
        if ($id == '' || $show == '') {
            $this->error('发布失败!');
        } else {
            $data['show'] = $show;
            $data['modify_uid'] = UID;
            $data['modify_time'] = date('Y-m-d H:i:s');
            $res = M('Main_info')->where('id=' . $id)->data($data)->save();
            if ($res) {
                $this->success('发布成功!');
            } else {
                $this->error('发布失败!');
            }
        }
    }

    public function packto()
    {
        $id = I('id', '');
        $cg = I('cg', '0');
        if ($id == '' || $cg == '') {
            $this->error('发布失败!');
        } else {
            if ($cg == 2) {
                $name = '今日推荐';
            } else if ($cg == 3) {
                $name = '明日推荐';
            }
            $cg_id = M('Category')->where(array('title' => $name))->field('id')->find();
            $order = M('Main_info')->where('id=' . $id)->find();
            if ($order && $cg_id) {
                $message = M('Info_detail')->where('info_id=' . $order['id'])->field('message')->find();
                $document['uid'] = UID;
                $document['title'] = $order['title'];
                $document['category_id'] = $cg_id['id'];
                $document['model_id'] = 2;
                $document['status'] = 1;
                $document['create_time'] = time();
                $document['update_time'] = time();
                $content = '<table class="table table-bordered">
                <tbody>
                    
                    <tr class="num2">
                        <td class="title_text"><strong>联系电话</strong></td>
                        <td>' . $order['telphone'] . '</td>
                    </tr>
                    <tr class="num1">
                        <td class="title_text"><strong>公司名称</strong></td>
                        <td>' . $order['company'] . '</td>
                    </tr>
                    
                    <tr class="num1">
                        <td class="title_text"><strong>客户类型</strong></td>
                        <td>' . $order['type'] . '</td>
                    </tr>
                    
                    <tr class="num2">
                        <td class="title_text"><strong>补充内容</strong></td>
                        <td>' . $message['message'] . '</td>
                    </tr>
                    </tbody></table>';
                $res = M('Document')->data($document)->add();
                if ($res){
                    $article['id'] = $res;
                    $article['content'] = $content;
                    $cons = M('Document_article')->data($article)->add();
                    if ($cons){
                        $this->success('发布成功!');
                    }else{
                        $this->error('发布失败!');
                    }
                }else{
                    $this->error('发布失败!');
                }
            }else{
                $this->error('发布失败!');
            }
        }
    }
}